import 'package:flutter/material.dart';

//语言国际化-应用内设置语言工具类
//使用：
//应用内切换语言：
//freeLocalizationStateKey.currentState.changeLocale(const Locale('zh',"CH"));

GlobalKey<_FreeLocalization> freeLocalizationStateKey = new GlobalKey<_FreeLocalization>();

class FreeLocalization extends StatefulWidget{

  final Widget child;

  FreeLocalization({Key key,this.child}):super(key:key);

  @override
  State<FreeLocalization> createState() {
    return _FreeLocalization();
  }
}

class _FreeLocalization extends State<FreeLocalization>{

  //默认设置:英文
  Locale _locale = const Locale('en','US');

  //修改语言
  changeLocale(Locale locale){
    setState((){
      _locale = locale;
    });
  }

  //包装Widget，使Widget响应语言修改
  @override
  Widget build(BuildContext context) {
    return new Localizations.override(
      context: context,
      locale: _locale,
      child: widget.child,
    );
  }
}